import api from './config'

export interface ChatRecord {
  type: 'user' | 'assistant'
  content: string
}

export interface SessionInfo {
  sessionId: string
  createTime: string
  updateTime: string
  title: string
  lastMessage: string
}

export const chatApi = {
  // 获取会话列表
  getSessionList: async (userId: string, characterId: string): Promise<{ data: SessionInfo[] }> => {
    return api.get(`/ai/chat/sessions/${userId}/${characterId}`)
  },

  // 加载默认会话历史
  getDefaultHistory: async (userId: string, characterId: string): Promise<{ data: ChatRecord[] }> => {
    return api.get(`/ai/chat/history/${userId}/${characterId}`)
  },

  // 加载指定会话历史
  getSessionHistory: async (sessionId: string): Promise<{ data: ChatRecord[] }> => {
    return api.get(`/ai/chat/history/session/${sessionId}`)
  },

  // 创建新会话
  newSession: async (characterId: string, title: string): Promise<{ data: string }> => {
    return api.post(`/ai/chat/new/${characterId}`, { title })
  },

  // 发送消息
  sendMessage: async (params: Record<string, any>): Promise<any> => {
    // 覆盖 baseURL，保持与页面内原逻辑一致
    return api.get('/ai/chat', {
      baseURL: '' as any,
      params
    })
  }
}